package com.gd.server.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.gd.common.entities.S_AddFriendReq;
import com.gd.common.entities.S_OfflineMessage;
import com.gd.common.util.ConnectionUtil;
import com.gd.server.dao.AddFriendDao;



public class AddFriendDaoImpl implements AddFriendDao{

	@Override
	public void insertFriendRequest(int fromUserId, int toUserId, int type) throws Exception {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		conn = ConnectionUtil.getCurrentConnection();
		String sql_insert = "INSERT INTO gds_add_friend_req(from_user_id, to_user_id, time, type) "+
				   "values(?,?,?,?)";
		pstm=conn.prepareStatement(sql_insert);
		pstm.setInt(1, fromUserId);
		pstm.setInt(2, toUserId);
		pstm.setDouble(3, new Date().getTime());
		pstm.setInt(4, type);
		pstm.execute();	
	}

	@Override
	public List<S_AddFriendReq> getOfflineFriendReq(int userId) throws Exception {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		conn = ConnectionUtil.getCurrentConnection();
		String sql_select = "SELECT * FROM `gds_add_friend_req` WHERE `to_user_id`=?";
		pstm = conn.prepareStatement(sql_select);
		pstm.setInt(1, userId);
		rs = pstm.executeQuery();
		
		List<S_AddFriendReq> offlineFriendReq = new ArrayList<S_AddFriendReq>();
		while(rs.next()){
			S_AddFriendReq friendReq = new S_AddFriendReq();
			friendReq.setId(rs.getInt(1));
			friendReq.setFrom_user_id(rs.getInt(2));
			friendReq.setTo_user_id(rs.getInt(3));
			friendReq.setTime((long)rs.getDouble(4));
			friendReq.setType(rs.getInt(5));
			offlineFriendReq.add(friendReq);
		}
		return offlineFriendReq;
	}

	
}
